<?php
/** 
 *
 */
 
namespace osc\mobile\controller;
use think\Db;
use think\Session;

class Stoncer extends MobileBase
{
	protected function _initialize(){
		parent::_initialize();						
		define('UID',is_login());	
		//手机版
		if(!UID){		
			$this->redirect('login/login');					
		}

		if (!Session::has("username")) {

            $this->redirect('login/login');

        }		
	}
	
	function index(){


		if (!Session::has("username")) {

            $this->redirect('login/login');

        }	
        $username = session('username');
        $userinfo = Db::name('member')->where('username',$username)->find();
        
        //查询用户持有的
        $userstone = Db::name('user_stone')->where('username',$username)->select();

        //查询当前行情
        $stone = Db::name('stone')->select();

		$stone_position = Db::name('stone_position')->where('username',$username)->find();


	
		$this->assign('userstone',$userstone);
		$this->assign('stone',$stone);	
		$this->assign('pos',$stone_position);

		$this->assign('top_title','增值中心');
		$this->assign('SEO',['title'=>'增值中心-'.config('SITE_TITLE')]);
		
		$this->assign('flag','user');
		
		return $this->fetch();
	}

	function buy()
	{
	   $data=input('get.'); 
	   $id = "";
	   $username = session('username');
		if($data){
				if($data['id'])
				{
					if(is_numeric($data['id']))
					{
						$id = $data['id'];
					}
					else
					{

            			$this->redirect('stoncer/index');
					}
				}
		}

        $post_data = input('post.');

        if($post_data)
        {
        	if(!$post_data['id'])
        	{
        		$this->redirect('stoncer/index');
        	}

        	if(is_numeric($post_data['id']))
			{
				$id = $post_data['id'];
			}
			
			//判断是否在交易时间段
            $curr_hi =(int) date("H");
            //var_dump($curr_hi);

            $flag = $this->isexchange($curr_hi);
            //$flag = 1;
            

            if($flag == 0)
            {
            	return array('error'=>'不在处理时间段','url'=>'/mobile/stoncer');
            }

            $post_data['token']  = input('post.__token__/s', null);

            $r=osc_service('common','Stone')->stone_goldstone($post_data);
            if(isset($r['error'])){
				return $r;
			}	
			return array('success'=>'提交操作成功','url'=>'/mobile');


        }

        //购买记录

        $stone = Db::name('stone_goldstone_tf')->where('username',$username)->select();

		$this->assign('top_title','参与增值');
		$this->assign('id',$id);
		$this->assign('list',$stone);
		$this->assign('SEO',['title'=>'参与增值-'.config('SITE_TITLE')]);
		
		$this->assign('flag','user');
		return $this->fetch();


	}

	function isexchange($curr_hi)
	{
		    $flag = 0;
		    if($curr_hi>=9)
            {
            	if($curr_hi<11)
            	{
            		$flag = 1;
            	}
            }

            if($curr_hi >=13)
            {
            	if($curr_hi<15)
            	{
            		$flag = 1;

            	}
            }

            if($curr_hi >=20)
            {
            	if($curr_hi<22)
            	{
            		$flag = 1;

            	}
            }

            return $flag;
	}


	function sell()
	{
 		

	   $data=input('get.'); 
	   $id = "";
	   $username = session('username');
		if($data){
				if($data['id'])
				{
					if(is_numeric($data['id']))
					{
						$id = $data['id'];
					}
					else
					{

            			$this->redirect('stoncer/index');
					}
				}
		}

        $post_data = input('post.');

        if($post_data)
        {
        	if(!$post_data['id'])
        	{
        		$this->redirect('stoncer/index');
        	}

        	if(is_numeric($post_data['id']))
			{
				$id = $post_data['id'];
			}
			
			//判断是否在交易时间段
            $curr_hi =(int) date("H");
            //var_dump($curr_hi);

            //$flag = $this->isexchange($curr_hi);
            $flag = 1;
            

            if($flag == 0)
            {
            	return array('error'=>'不在处理时间段','url'=>'/mobile/stoncer');
            }

            $post_data['token']  = input('post.__token__/s', null);

            //$r=osc_service('common','Stone')->stone_sell($post_data);
            $r=osc_service('common','Stone')->stone_sell($post_data);
            if(isset($r['error'])){
				return $r;
			}	
			return array('success'=>'提交操作成功','url'=>'/mobile');


        }

        //购买记录

        $stone = Db::name('stone_position_tf')->where('username',$username)->select();

		$this->assign('top_title','出售');
		$this->assign('id',$id);
		$this->assign('list',$stone);
		$this->assign('SEO',['title'=>'出售-'.config('SITE_TITLE')]);
		
		$this->assign('flag','user');
		return $this->fetch();

	}

	function wd()
	{


		if(request()->isPost()){
			$data=input('post.');
			//var_dump($data);
			$data['token']  = input('post.__token__/s', null);
			//var_dump($data);
			$r=osc_service('common','Stone')->stone_wd($data);
			if(isset($r['error'])){
				return $r;
			}

			return array('success'=>'申请回购成功','url'=>'/mobile');
		}

		$username = session('username');
		$userinfo = Db::name('member')->where('username',$username)->select();
		$list = Db::name('stonewd')->where('username',$username)->order('creat_time desc')->limit(50)->select();
		$bank = Db::name('user_bank')->where('username',$username)->find();

		$flag='0';
		if(empty($bank))
		{
			$flag='1';
		}else{

			if(empty($bank['subbankname']))
			{			
				$flag='1';
			}
			if($bank['subbankname']=='')
			{			
				$flag='1';
			}
		}
		$stone_position = Db::name('stone_position')->where('username',$username)->find();
		$this->assign('position',$stone_position);
		$this->assign('flag',$flag);
		$this->assign('userinfo',$userinfo);
		$this->assign('bank',$bank);
        $this->assign('list',$list);
		$this->assign('top_title','增值回购');
		$this->assign('SEO',['title'=>'增值回购-'.config('SITE_TITLE')]);
				
		return $this->fetch();
	}

	function stone_tf()
	{

	}

	
}
